/*
 * @copyright 2010 Gerald Jacobson
 * @license GNU General Public License
 * 
 * This file is part of Descent Assistant.
 *
 * Descent Assistant is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Descent Assistant is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Descent Assistant.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.amphiprion.descenttool.task;

import java.util.ArrayList;
import java.util.List;

import org.amphiprion.descenttool.R;
import org.amphiprion.descenttool.dao.MonsterDao;
import org.amphiprion.descenttool.entity.AvailableMonster;
import org.amphiprion.descenttool.entity.Monster;
import org.amphiprion.descenttool.util.PreferenceUtil;
import org.amphiprion.descenttool.util.PreferenceUtil.Preference;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;

/**
 * @author amphiprion
 * 
 */
public class AddMonsterTask extends AsyncTask<Void, Integer, List<Monster>> {
	private AddMonsterListener caller;
	private List<AvailableMonster> availableMonsters;
	private ProgressDialog progress;
	private String level;

	/**
	 * Default constructor.
	 */
	public AddMonsterTask(AddMonsterListener caller, List<AvailableMonster> availableMonsters, String level) {
		this.caller = caller;
		this.availableMonsters = availableMonsters;
		this.level = level;
	}

	@Override
	protected List<Monster> doInBackground(Void... v) {
		try {
			if (PreferenceUtil.getInt(Preference.ENCOUNTER_MODE, 0) == 0) {
				List<Monster> selectedMonsters = new ArrayList<Monster>();
				for (int i = 0; i < availableMonsters.size(); i++) {
					final AvailableMonster avmonster = availableMonsters.get(i);
					if (!avmonster.checked) {
						continue;
					}
					final Monster monster = MonsterDao.getInstance().getMonster(avmonster.id, level);
					if (monster != null) {
						selectedMonsters.add(monster);
					}
				}
				return selectedMonsters;
			} else if (PreferenceUtil.getInt(Preference.ENCOUNTER_MODE, 0) == 1) {
				// BASIC MODE
				List<Monster> selectedMonsters = new ArrayList<Monster>();
				for (int i = 0; i < availableMonsters.size(); i++) {
					final AvailableMonster avmonster = availableMonsters.get(i);
					if (!avmonster.checked) {
						continue;
					}
					final Monster monster = MonsterDao.getInstance().getMonster(avmonster.id, "" + PreferenceUtil.getInt(Preference.NB_PLAYER, 5) + "p");
					if (monster != null) {
						selectedMonsters.add(monster);
					}
				}
				return selectedMonsters;

			} else if (PreferenceUtil.getInt(Preference.ENCOUNTER_MODE, 0) == 2) {
				// CAMPAIGN
				List<Monster> selectedMonsters = new ArrayList<Monster>();
				for (int i = 0; i < availableMonsters.size(); i++) {
					final AvailableMonster avmonster = availableMonsters.get(i);
					if (!avmonster.checked) {
						continue;
					}
					Monster monster = null;
					String clazz = avmonster.clazz;
					int level = 0;
					if ("beast".equals(clazz)) {
						// BEAST
						level = PreferenceUtil.getInt(Preference.LEVEL_BEAST, 0);
					} else if ("humanoid".equals(clazz)) {
						// HUMANOID
						level = PreferenceUtil.getInt(Preference.LEVEL_HUMANOID, 0);
					} else if ("eldritch".equals(clazz)) {
						// ELDRITCH
						level = PreferenceUtil.getInt(Preference.LEVEL_ELDRITCH, 0);
					} else {
						// LIEUTENANT
						level = PreferenceUtil.getInt(Preference.LEVEL_LIEUTENANT, 0);
					}
					String sLevel = "";
					if (level == 0) {
						sLevel = "copper";
					} else if (level == 1) {
						sLevel = "silver";
					} else if (level == 2) {
						sLevel = "gold";
					} else if (level == 3) {
						sLevel = "diamond";
					}
					monster = MonsterDao.getInstance().getMonster(avmonster.id, sLevel);
					if (monster != null) {
						selectedMonsters.add(monster);
						// Log.d(ApplicationConstants.PACKAGE, ">" +
						// monster.name);
					}
				}
				return selectedMonsters;

			}
		} catch (Exception e) {

		}
		return null;
	}

	@Override
	protected void onPreExecute() {
		progress = ProgressDialog.show(caller.getContext(), "", caller.getContext().getString(R.string.wait), true, true, new DialogInterface.OnCancelListener() {
			@Override
			public void onCancel(DialogInterface dialog) {
				cancel(true);
			}
		});
	}

	@Override
	protected void onProgressUpdate(Integer... values) {
	}

	@Override
	protected void onPostExecute(List<Monster> monsters) {
		if (progress != null) {
			progress.dismiss();
		}
		caller.importEnded(!isCancelled() && monsters != null, monsters);
	}

	public interface AddMonsterListener {
		void importEnded(boolean succeed, List<Monster> monsters);

		Context getContext();
	}

}
